package servlet.search;

import com.alibaba.fastjson.JSON;
import pojo.Book;
import pojo.Book2;
import service.ConvertBooksType;
import service.SearchIndexpagere;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/bookDetail")
public class BookDetail extends HttpServlet {
    private ConvertBooksType convertBooksType = new ConvertBooksType();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置响应对象类型
        resp.setContentType("application/json;charset=utf-8");
        // 从请求中获取参数
        String id = req.getParameter("id");
        System.out.println(id);
        SearchIndexpagere searchIndexpagere = new SearchIndexpagere();
        Book book = searchIndexpagere.selectBookDetail(id);
        Book2 book2 = convertBooksType.parseBook(book);
        String jsonString = JSON.toJSONString(book2);
        System.out.println("图书详细服务中打印的id值："+book.getId());
        PrintWriter writer = resp.getWriter();
        writer.write(jsonString);
    }
}
